package com.gs.SynPatrolRep.util;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.gs.SynPatrolRep.R;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DateUtil {

    private static volatile DateUtil instance;

    public static DateUtil getInstance() {
        if (instance == null) {
            synchronized (DateUtil.class) {
                if (instance == null) {
                    instance = new DateUtil();
                }
            }
        }
        return instance;
    }

    public DateUtil() {
    }

    public interface ShowTimeSelectorCallback {
        void OnTimeSelectListener(Date date);
    }

//    public void showDateSelector(Context context, View v, Calendar selectCalendar, final ShowTimeSelectorCallback callback) {
//        showDateSelector(context, v, selectCalendar,  "yyyyMMdd", callback);
//    }
//
    public void showDateSelector(Context context,View v, Calendar selectCalendar, String format,final ShowTimeSelectorCallback callback) {
        WindowUtil.getInstance().hideSoftInput(v);
        TimePickerBuilder builder = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                if (callback != null) {
                    callback.OnTimeSelectListener(date);
                }
            }
        });
        Calendar startDate = Calendar.getInstance();
        startDate.set(2000, 1, 01);
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.set(calendarEnd.get(Calendar.YEAR) + 10, 12, 31);
        TimePickerView timePickerView = builder
                .setType(new boolean[]{  format.contains("yyyy"),
                        format.contains("MM"),
                        format.contains("dd"),
                        format.contains("HH"),
                        format.contains("mm"),
                        format.contains("ss")})
                .setLabel(
                        format.contains("yyyy") ? context.getString(R.string.year) : "",
                        format.contains("MM") ? context.getString(R.string.month) : "",
                        format.contains("dd") ? context.getString(R.string.day) : "",
                        format.contains("HH") ? context.getString(R.string.hours) : "",
                        format.contains("mm") ? context.getString(R.string.mins) : "",
                        format.contains("ss") ? context.getString(R.string.seconds) : "")
                .isCenterLabel(true).setDividerColor(Color.DKGRAY).setCancelText(context.getString(R.string.cancel))
                .setSubmitText(context.getString(R.string.confirm))
                .setLineSpacingMultiplier(3.0f)
                .setItemVisibleCount(7)
                .setDecorView(null)
                .setRangDate(startDate, calendarEnd)
                .build();
        timePickerView.setDate(selectCalendar);
        timePickerView.show();
    }

    public String DateTimeToStr(Date date) {
        return DateTimeToStr(date, "yyyy-MM-dd");
    }

    public String DateTimeToStr(Date date, String format) {
        //可根据需要自行截取数据显示
        if (date == null)
            return "";
        SimpleDateFormat simpleDateFormat = null;
        try {
            simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return simpleDateFormat.format(date);
    }

    public Date StrToDateTime(String str){
        return StrToDateTime(str,"yyyy-MM-dd");
    }

    public Date StrToDateTime(String str,String format){
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getTimeFormatText(Date date) {
        // 一分鐘
        long minute = 60 * 1000;
        // 一小時
        long hour = 60 * minute;
        // 一天
        long day = 24 * hour;
        // 一個月
        long month = 31 * day;
        // 一年
        long year = 12 * month;
        if (date == null) {
            return null;
        }
        long diff = new Date().getTime() - date.getTime();
        long r;
        if (diff > year) {
            r = diff / year;
            return r + "年前";
        }
        if (diff > month) {
            r = diff / month;
            return r + "個月前";
        }
        if (diff > day) {
            r = diff / day;
            return r + "天前";
        }
        if (diff > hour) {
            r = diff / hour;
            return r + "小時前";
        }
        if (diff > minute) {
            r = diff / minute;
            return r + "分鐘前";
        }
        return "剛剛";
    }

    /**
     * 自定义选择列表
     *
     * @param context
     * @param onSelectorCallback
     * @return
     */
    public static OptionsPickerView<String> showDate(Context context, final OnSelectorCallback onSelectorCallback) {
        return new OptionsPickerBuilder(context,
                new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        if (onSelectorCallback != null) {
                            onSelectorCallback.onSelectListener(options1, options2, options3);
                        }
                    }
                })
                .setCancelText(context.getResources().getString(R.string.cancel))
                .setCancelColor(context.getResources().getColor(R.color.color_666))
                .setSubmitText(context.getResources().getString(R.string.ok))
                .setSubmitColor(context.getResources().getColor(R.color.btn_back))
                .setTitleBgColor(context.getResources().getColor(R.color.white))
                .isDialog(false)
                .setDividerColor(context.getResources().getColor(R.color.color_border))
                .setLineSpacingMultiplier(3.0f)
                .build();
    }

    public interface OnSelectorCallback {
        void onSelectListener(int options1, int options2, int options3);
    }

    /**
     * 获取时间日期列表
     *
     * @param c
     * @return
     */
    public static List<List<String>> getDateList(Calendar c) {
        List<List<String>> list = new ArrayList<>();
        List<String> times = new ArrayList<>();
        List<String> pos = new ArrayList<>();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = sdf.parse(year - 1 + "-01-01");
            Date endDate = sdf.parse(year + 1 + "-12-31");
            Date nowDate = sdf.parse(year + "-" + month + "-" + day);
            c.setTime(startDate);
            while (c.getTime().compareTo(endDate) <= 0) {
                String time = sdf.format(c.getTime());
                times.add(time);
                if ((c.getTime().compareTo(nowDate)) == 0) {
                    pos.add(String.valueOf(times.size() - 1));
                }
                c.add(Calendar.DATE, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        list.add(times);
        list.add(pos);
        return list;
    }

    /**
     * 获取保持两位数的列表
     *
     * @param num
     * @return
     */
    public static List<String> getNumList(int num) {
        List<String> list = new ArrayList<>();
        DecimalFormat decimalFormat = new DecimalFormat("00");
        for (int i = 0; i < num; i++) {
            list.add(decimalFormat.format(i));
        }
        return list;
    }
}
